<?php
class sfBreadcrumbYamlHandler {
	
	/**
	* Initialize the sfBreadcrumbs instance
	*
	* @return array an array of sfBreadcrumbModule objects
	*/
	public static function init() {
		
		// load required helpers
		sfContext::getInstance()->getConfiguration()->loadHelpers(array('I18N'));

		$breadcrumbs = sfConfig::get('app_breadcrumbs');
		
		$modules = array();

		// file contains predefined rules
		if( count($breadcrumbs) > 0 ){

			// loop over modules
			foreach( $breadcrumbs['all'] as $module => $module_data ) {
			
				// create a new sfBreadcrumbModule instance
				$breadcrumb_module = new sfBreadcrumbModule($module, __($module_data['name']));
				
				// set the url of the module if available
				if (isset($module_data['url'])) {
					
					$breadcrumb_module->setUrl($module_data['url']);
					
				}
				
				// this module's sections
				$sections = array();
				
				// loop over sections
				foreach($module_data['sections'] as $section => $section_data) {

					// create a new sfBreadcrumbSection instance
					$breadcrumb_section = new sfBreadcrumbSection($section, __($section_data['name']), $breadcrumb_module);
				
					// this section's actions
					$actions = array();
				
					// loop over actions
					foreach($section_data['actions'] as $action => $action_data) {
					
						// create a new sfBreadcrumbAction instance
						$breadcrumb_action = new sfBreadcrumbAction($action, __($action_data['name']), $breadcrumb_section);
					
						$parameters = array();
						
						// add parameters if available
						if( isset($action_data['params']) ){
						
							// loop over parameters
							foreach($action_data['params'] as $param => $param_name) {
						
								// create a new sfBreadcrumbActionParameter instance
								$breadcrumb_param = new sfBreadcrumbActionParameter($param, $param_name, $breadcrumb_action);
						
								// add parameter to action's parameters list
								$parameters[] = $breadcrumb_param;
						
							}
					
							// set action's parameters
							$breadcrumb_action->setParameters($parameters);
						
						}
					
						// add action to section's actions list
						$actions[] = $breadcrumb_action;
					
					}
				
					// set section's actions
					$breadcrumb_section->setActions($actions);
				
					// add section to module's sections list
					$sections[] = $breadcrumb_section;
				
				}
			
				// set modules's sections
				$breadcrumb_module->setSections($sections);
			
				// add module to the modules list
				$modules[] = $breadcrumb_module;
			
			}
		
		} else {
			
			// scan modules directory of the current app to get the list of available modules names
			$modules_dir = scandir( sfConfig::get('sf_app_dir').'/modules' );
			
			foreach($modules_dir as $name) {
				
				if( $name != '.' && $name != '..' ){
				
					// create a new sfBreadcrumbModule instance
					$breadcrumb_module = new sfBreadcrumbModule($name, ucfirst($name));
					
					$modules[] = $breadcrumb_module;
				
				}
				
			}
			
		}
		
		return $modules;
		
	}
	
	/**
	* Save an array of sfBreadcrumbModule objects to the breadcrumbs.yml file
	*
	* @param array an array of sfBreadcrumbModule objects
	*/
	public static function dump(array $modules) {
		
		// main array that will contain the data to be dumped to the yaml file
		$all = array();
		
		// loop over modules
		foreach($modules as $module){
			
			// module data
			$module_data = array();
			
			// set module display name
			$module_data['name'] = $module->getDisplayName();
			
			if ($module->getUrl() != null) {
				
				$module_data['url'] = $module->getUrl();
				
			}
			
			// loop over sections
			foreach($module->getSections() as $section) {
				
				// section data
				$section_data = array();
			
				// set section display name
				$section_data['name'] = $section->getDisplayName();
				
				// loop over actions
				foreach($section->getActions() as $action) {
					
					// action data
					$action_data = array();
					
					// set action display name
					$action_data['name'] = $action->getDisplayName();
					
					// loop over parameters
					foreach($action->getParameters() as $parameter) {
						
						$action_data['params'][$parameter->getSymfonyName()] = $parameter->getDisplayName();
						
					}
					
					// add actions to its parent section
					$section_data['actions'][$action->getSymfonyName()] = $action_data;
					
				}
				
				// add section to the its parent module
				$module_data['sections'][$section->getSymfonyName()] = $section_data;
				
			}
			
			// add module to the main array
			$all['all'][$module->getSymfonyName()] = $module_data;
			
		}
		
		$file = sfConfig::get('sf_app_config_dir').'/breadcrumbs.yml';
		
		$yaml = sfYaml::dump( $all );
		
		file_put_contents( $file, $yaml );
		
	}
	
}
